
package org.sxp.common.config;

import com.google.common.collect.Maps;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.sxp.common.xss.XssFilter;

import javax.servlet.DispatcherType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Filter配置
 *
 *
 */
@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean shiroFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new DelegatingFilterProxy("shiroFilter"));
        //该值缺省为false，表示生命周期由SpringApplicationContext管理，设置为true则表示由ServletContainer管理
        registration.addInitParameter("targetFilterLifecycle", "true");
        registration.setEnabled(true);
        registration.setOrder(Integer.MAX_VALUE - 1);
        registration.addUrlPatterns("/*");
        return registration;
    }

    @Bean
    public FilterRegistrationBean xssFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setDispatcherTypes(DispatcherType.REQUEST);
        registration.setFilter(new XssFilter());
        registration.addUrlPatterns("/*");
        registration.setName("xssFilter");
        registration.setOrder(Integer.MAX_VALUE);

        Map<String, Object> initParameters = Maps.newHashMap();
        List<String> excludes = new ArrayList<>();
        excludes.add("/datasource/database/fieldOfTable");
        excludes.add("/dataworks/sync/update");
        excludes.add("/dataworks/sync/save");
        excludes.add("/dataworks/sync/execute");
        excludes.add("/jobinfo/update");
        excludes.add("/jobinfo/add");
        excludes.add("/datasource/database/testConn");
        excludes.add("/datasource/database/save");
        excludes.add("/datasource/database/update");
        excludes.add("/activiti/model/*");
        initParameters.put("excludes", String.join(",", excludes));

        registration.setInitParameters(initParameters);
        return registration;
    }
}
